package com.example.controller;

import com.example.entity.RestBean;
import com.example.entity.dto.AccountDetailsDto;
import com.example.entity.dto.AccountDto;
import com.example.entity.vo.request.*;
import com.example.entity.vo.response.AccountDetailsVo;
import com.example.entity.vo.response.AccountVo;
import com.example.service.AccountDetailsService;
import com.example.service.AccountPrivacyService;
import com.example.service.AccountService;
import com.example.utils.Const;
import com.example.utils.ControllerUtils;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

import static com.example.utils.Const.USER_ID;

@RestController
@RequestMapping("/api/user")
public class AccountController {

    @Resource
    private AccountService accountService;

    @Resource
    private AccountDetailsService accountDetailsService;

    @Resource
    private ControllerUtils controllerUtils;

    @Resource
    private AccountPrivacyService accountPrivacyService;

    /**
     * 查询用户基本信息
     * @param id 用户id
     * @return 用户信息
     */
    @GetMapping("/info")
    public RestBean<AccountVo> info(@RequestAttribute(USER_ID)int id){
        AccountDto accountDto = accountService.findAccountById(id);
        AccountVo accountVo = new AccountVo();
        BeanUtils.copyProperties(accountDto,accountVo);
        return RestBean.success(accountVo);
    }

    /**
     * 查找用户详细信息
     * @param id 用户Id
     * @return 用户详细信息
     */
    @GetMapping("/details")
    public RestBean<AccountDetailsVo> details(@RequestAttribute(USER_ID) int id){
//        AccountDetailsVo vo = new AccountDetailsVo();
        AccountDetailsDto details = Optional
                .ofNullable(accountDetailsService.findAccountDetailsById(id))
                .orElseGet(AccountDetailsDto::new);
        //dto转vo
//        BeanUtils.copyProperties(dto,vo);
//        该方法在如果dto中有空属性会报错
//        且vo不可以集成其他类
        return RestBean.success(details.asViewObject(AccountDetailsVo.class));
    }

    /**
     * 更新用户详细信息
     * @param id 当前登录用户Id
     * @param vo 用户信息
     * @return 结果
     */
    @PostMapping("/save-details")
    public RestBean<Void> saveDetails(@RequestAttribute(USER_ID) int id,
                                      @RequestBody @Valid DetailsSaveVo vo){
        boolean success = accountDetailsService.saveAccountDetails(id, vo);
        return success ? RestBean.success() : RestBean.failure(400, "此用户名已被占用");
    }

    /**
     * 修改绑定邮箱
     * @param id 当前用户Id
     * @param vo 修改的邮箱
     * @return 结果
     */
    @PostMapping("/modify-email")
    public RestBean<Void> modifyEmail(@RequestAttribute(USER_ID) int id,
                                      @RequestBody @Valid ModifyEmailVo vo){
        return controllerUtils.messageHandle(() -> accountService.modifyEmail(id, vo));
    }

    /**
     * 修改密码
     * @param id 当前用户Id
     * @param vo 新旧密码
     * @return 结果
     */
    @PostMapping("/change-password")
    public RestBean<Void> changePassword(@RequestAttribute(USER_ID) int id,
                                         @RequestBody @Valid ChangePasswordVo vo){
        return controllerUtils.messageHandle(() -> accountService.changePassword(id, vo));
    }

    /**
     * 保存隐私设置
     * @param id 用户Id
     * @param vo 目标隐私选项
     * @return 结果
     */
    @PostMapping("/save-privacy")
    public RestBean<Void> savePrivacy(@RequestAttribute(USER_ID) int id,
                                      @RequestBody @Valid PrivacySaveVo vo){
        accountPrivacyService.saveAccountPrivacy(id,vo);
        return RestBean.success();
    }

    /**
     * 读取用户隐私设置
     * @param id 用户Id
     * @return 结果
     */
    @GetMapping("/privacy")
    public RestBean<AccountPrivacyVo> privacy(@RequestAttribute(USER_ID) int id){
        return RestBean.success(accountPrivacyService.getAccountPrivacyById(id).asViewObject(AccountPrivacyVo.class));
    }


}
